package com.book.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.config.JwtUtil;
import com.book.mapper.BookDao;
import com.book.mapper.CollectRankingDao;
import com.book.mapper.LabelDao;
import com.book.model.Book;
import com.book.model.CollectRanking;
import com.book.model.Label;
import com.book.result.Result;
import com.book.service.LabelService;
import io.jsonwebtoken.Claims;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("youlike")
@SpringBootTest
public class YouLikeController {

    @Autowired
    private LabelService labelService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private CollectRankingDao collectRankingDao;

//
    @GetMapping("GetYouLike")
    public Result GetYouLike(@RequestHeader String unauthorized){
        labelService.GetYouLike(unauthorized);
        return Result.OK();
    }

    @Test
    public void Test(){
        Claims claims = jwtUtil.parseJwt("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI1Iiwic3ViIjoie1wiaWRcIjo1LFwicGFzc3dvcmRcIjpcIjgxZDA3NjU4NGRmZGQ2YTI5YTNlY2YzNDRiYWJlZjU1XCIsXCJzYWx0XCI6XCI2MzA2MDg0NlwiLFwidXNlcm5hbWVcIjpcImxpc2kxNlwifSIsImlhdCI6MTY5NzUxMDkwMSwicm9sZXMiOiJyb2xlMSIsImV4cCI6MTY5NzUxMTkwMX0.-IWzsJQmnaqCFBy-ENtbKulrwYQtY-T6Aiz4aRNNxhE");
        String id = claims.getId();
        redisTemplate.boundZSetOps("TAG_NUM_COUNT_" + id);
        if(redisTemplate.hasKey("TAG_NUM_COUNT_"+id)){
            System.out.println("已注册并已阅小说");
            ArrayList<List<Label>> labels = new ArrayList<>();
            HashSet<Object> hashSet = new HashSet<>();
            BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps("TAG_NUM_COUNT_" + id);
            Set set = boundZSetOperations.reverseRange(0, 2);
            Object collect = set.stream().map((x) -> {
                QueryWrapper<Label> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("label1",x);
                List<Label> labels1 = labelDao.selectList(queryWrapper1);
                QueryWrapper<Label> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("label2",x);
                List<Label> labels2 = labelDao.selectList(queryWrapper2);
                QueryWrapper<Label> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("label3",x);
                List<Label> labels3 = labelDao.selectList(queryWrapper3);
                labels.add(labels1);
                labels.add(labels2);
                labels.add(labels3);
                return labels;
            }).collect(Collectors.toList());
            labels.stream().map((x)->{
                for (Label label : x) {
                    Integer id1 = label.getId();
                    Book book = bookDao.selectById(id1);
                    String name = book.getName();
                    hashSet.add(name);
                }
                return x;
            }).collect(Collectors.toList());
            System.out.println("猜你喜欢小说:"+hashSet);
        }else{
            System.out.println("未注册且未阅小说");
            QueryWrapper<CollectRanking> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("number").last("limit 3");
            List<CollectRanking> collectRankings = collectRankingDao.selectList(queryWrapper);
            List<List<Label>> book_id = collectRankings.stream().map((x) -> {
                QueryWrapper<Label> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("book_id", x.getBookId());
                List<Label> labels = labelDao.selectList(queryWrapper1);
                return labels;
            }).collect(Collectors.toList());
            book_id.stream().map((x)->{
                Label label = x.get(0);
                redisTemplate.boundZSetOps("TAG_NUM_COUNT_"+id).incrementScore(label.getLabel1(),1);
                redisTemplate.boundZSetOps("TAG_NUM_COUNT_"+id).incrementScore(label.getLabel2(),1);
                redisTemplate.boundZSetOps("TAG_NUM_COUNT_"+id).incrementScore(label.getLabel3(),1);
                return x;
            }).collect(Collectors.toList());
            ArrayList<List<Label>> labels = new ArrayList<>();
            HashSet<Object> hashSet = new HashSet<>();
            BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps("TAG_NUM_COUNT_" + id);
            Set set = boundZSetOperations.reverseRange(0, 2);
            Object collect = set.stream().map((x) -> {
                QueryWrapper<Label> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("label1",x);
                List<Label> labels1 = labelDao.selectList(queryWrapper1);
                QueryWrapper<Label> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("label2",x);
                List<Label> labels2 = labelDao.selectList(queryWrapper2);
                QueryWrapper<Label> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("label3",x);
                List<Label> labels3 = labelDao.selectList(queryWrapper3);
                labels.add(labels1);
                labels.add(labels2);
                labels.add(labels3);
                return labels;
            }).collect(Collectors.toList());
            labels.stream().map((x)->{
                for (Label label : x) {
                    Integer id1 = label.getId();
                    Book book = bookDao.selectById(id1);
                    String name = book.getName();
                    hashSet.add(name);
                }
                return x;
            }).collect(Collectors.toList());
            System.out.println("猜你喜欢小说:"+hashSet);
        }
    }
}
